package com.ttedu.action.manage.banks;

import java.util.Date;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.ttedu.base.BaseAction;
import com.ttedu.database.model.banks;
import com.ttedu.database.service.BanksService;
import com.ttedu.util.Validator;

public class BanksAction extends BaseAction{
	private banks bank=new banks();
	private String showid;
	@Autowired
	private BanksService banksservice;
	//展示
	@Action(value = "/cas/banks_index", results = { @Result(name = "success", location = "/WEB-INF/html/manage/banks/index.jsp") })
	public String banks_index() {
		return "success";
	}
	//添加
	@Action(value = "/cas/banks_add", results = { @Result(name = "success", location = "/WEB-INF/html/manage/banks/add.jsp") })
	public String banks_add() {
		String treeNodeid=request.getParameter("treeNodeid");
		if(Validator.isNull(treeNodeid)){
			treeNodeid="0";
		}else{
			banks banksone=banksservice.selectByPrimaryKey(Integer.valueOf(treeNodeid));
			request.setAttribute("parentname", banksone.getName());
			bank.setParentid(Integer.valueOf(treeNodeid));
		}
		request.setAttribute("treeNodeid", treeNodeid);
		return "success";
	}
	//显示
	@Action(value = "/cas/banks_show", results = { @Result(name = "success", location = "/WEB-INF/html/manage/banks/show.jsp") })
	public String banks_show() {
		String id=request.getParameter("id");
		bank=banksservice.selectByPrimaryKey(Integer.valueOf(id));
		int treeNodeid=bank.getParentid();
		if(treeNodeid==0){
			treeNodeid=0;
		}else{
			banks banksone=banksservice.selectByPrimaryKey(treeNodeid);
			request.setAttribute("parentname", banksone.getName());
		}
		request.setAttribute("treeNodeid", treeNodeid);
		return "success";
	}
	//保存或更新
	@Action(value = "/cas/banks_updateorsave", results = { @Result(name = "success",type="redirect", location = "/cas/banks_show.action?id=${showid}") })
	public String banks_updateorsave() {
		if(Validator.isNotNull(bank.getId())){
			bank.setUpdatedAt(new Date());
			banksservice.updateByPrimaryKey(bank);
		}
		//保存
		else{
			bank.setCreatedAt(new Date());
			bank.setUpdatedAt(new Date());
			if(Validator.isNull(bank.getParentid())){
				bank.setParentid(0);
			}
			banksservice.insert(bank);
		}
		showid=String.valueOf(bank.getId());
		return "success";
	}
	//删除
	@Action(value = "/cas/banks_delete", results = { @Result(name = "success", location = "/WEB-INF/html/manage/banks/success.jsp") })
	public String banks_delete() {
		banksservice.deleteByPrimaryKey(bank.getId());
		return "success";
	}
	//编辑
	@Action(value = "/cas/banks_edit", results = { @Result(name = "success", location = "/WEB-INF/html/manage/banks/edit.jsp") })
	public String banks_edit() {
		String id=request.getParameter("id");
		bank=banksservice.selectByPrimaryKey(Integer.valueOf(id));
		int treeNodeid=bank.getParentid();
		if(treeNodeid==0){
			treeNodeid=0;
		}else{
			banks banksone=banksservice.selectByPrimaryKey(treeNodeid);
			request.setAttribute("parentname", banksone.getName());
		}
		request.setAttribute("treeNodeid", treeNodeid);
		return "success";
	}
	
	
	public banks getBank() {
		return bank;
	}
	public void setBank(banks bank) {
		this.bank = bank;
	}
	public String getShowid() {
		return showid;
	}
	public void setShowid(String showid) {
		this.showid = showid;
	}
	
	
}
